﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Jian.Vubackup.MySql.Tool
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class LogWeb : ILog
    {
        /// <summary>
        /// 锁对象
        /// </summary>
        private object _lockHelper = new object();

        public void WriteInfo(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【INFO】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-info", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteDebug(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【DEBUG】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-debug", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteWarning(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【WARNING】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-warning", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteError(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【ERROR】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-error", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteException(Exception ex)
        {
            DateTime dt = DateTime.Now;
            string info = "【EXCEPTION】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 异常信息：" + ex.Message + "\r\n堆栈信息：" + ex.StackTrace;
            WriteLog(info, "log-exception", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteOnly(string content)
        {
            DateTime dt = DateTime.Now;
            string info = content;
            WriteLog(info, "log-only", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strContent">日志内容</param>
        /// <param name="dir">目录</param>
        /// <param name="filename">文件名称</param>
        private void WriteLog(string strContent, string dir, string filename)
        {
            try
            {
                lock (_lockHelper)
                {
                    string logFilePath = JTool.GetRootMapPath("logs");
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    logFilePath = Path.Combine(logFilePath, dir);
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    string file = Path.Combine(logFilePath, filename);
                    StreamWriter sw = new StreamWriter(file, true, Encoding.UTF8);
                    sw.WriteLine(strContent);
                    //sw.WriteLine("\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {

            }
        }

        private const string blank = "      ";
        public void WriteInfoAndConsole(string content)
        {
            WriteInfo(content);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("信息");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteDebugAndConsole(string content)
        {
            WriteDebug(content);

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.Write("调试");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteWarningAndConsole(string content)
        {
            WriteWarning(content);

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("警告");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();

        }

        public void WriteErrorAndConsole(string content)
        {
            WriteError(content);

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("错误");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteExceptionAndConsole(Exception ex)
        {
            WriteException(ex);

            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("异常");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + ex.Message + Environment.NewLine + ex.StackTrace);
            Console.ResetColor();
        }



        public void WriteOnlyAndConsole(string content)
        {
            WriteOnly(content);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(content);
            Console.ResetColor();
        }
    }
}
